package org.phoenix.mall.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.phoenix.mall.admin.common.pojo.bo.EditUserBO;
import org.phoenix.mall.admin.common.pojo.bo.UserBO;
import org.phoenix.mall.admin.common.pojo.vo.UserVO;
import org.phoenix.mall.admin.common.utils.PagedGridResult;
import org.phoenix.mall.admin.common.utils.PhoenixResponseJSON;
import org.phoenix.mall.admin.config.annotations.AuthRequired;
import org.phoenix.mall.admin.pojo.User;
import org.phoenix.mall.admin.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Api(value = "用户管理", tags = {"用户管理API"})
@RestController
@RequestMapping("users")
@AuthRequired
public class UserController {

    @Resource
    private UserService userService;


    @ApiOperation(value = "获取用户信息列表", notes = "获取用户信息列表", httpMethod = "GET")
    @GetMapping("")
    public PhoenixResponseJSON list(
            @ApiParam(name = "query", value = "查询条件")
            @RequestParam String query,
            @ApiParam(name = "pagenum", value = "页码")
            @RequestParam Integer pagenum,
            @ApiParam(name = "pagesize", value = "每页显示记录数")
            @RequestParam Integer pagesize) {


        if (pagenum == null) {
            pagenum = 1;
        }

        if (pagesize == null) {
            pagesize = 2;
        }
        PagedGridResult pagedGridResult = userService.getUserListByPage(pagenum, pagesize);

        return PhoenixResponseJSON.ok(pagedGridResult);
    }

    @ApiOperation(value = "添加用户", notes = "添加用户", httpMethod = "POST")
    @PostMapping("")
    public PhoenixResponseJSON add(@RequestBody UserBO userBO) {
        String username = userBO.getUsername();
        String password = userBO.getPassword();
        String mobile = userBO.getMobile();
        String email = userBO.getEmail();

        if (!(StringUtils.isNoneBlank(username) &&
                StringUtils.isNoneBlank() &&
                StringUtils.isNoneBlank() &&
                StringUtils.isNoneBlank())) {
            return PhoenixResponseJSON.errorMsg("用户信息不完善，请重新填写！");
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRoleId(1);
        user.setMobile(mobile);
        user.setEmail(email);
        user.setStatus(true);
        user.setCreateTime(new Date());
        UserVO userVO = userService.createUser(user);

        return PhoenixResponseJSON.ok(userVO);
    }

    @ApiOperation(value = "修改用户状态", notes = "修改用户状态", httpMethod = "PUT")
    @PutMapping("/{userId}/status/{statusVal}")
    public PhoenixResponseJSON edit(
            @ApiParam(name = "userId", value = "用户ID", required = true)
            @PathVariable Integer userId,
            @ApiParam(name = "statusVal", value = "修改的状态值", required = true)
            @PathVariable Boolean statusVal) {
        User user = new User();
        user.setId(userId);
        user.setStatus(statusVal);
        userService.editUser(user);

        return PhoenixResponseJSON.ok();
    }

    @ApiOperation(value = "获取用户信息", notes = "根据ID获取用户信息", httpMethod = "GET")
    @GetMapping("/{userId}")
    public PhoenixResponseJSON get(@PathVariable Integer userId) {
        UserVO user = userService.getUser(userId);
        if (user == null) {
            return PhoenixResponseJSON.errorMsg("无法获取相关用户信息！");
        }

        return PhoenixResponseJSON.ok(user);
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息", httpMethod = "PUT")
    @PutMapping("/{userId}")
    public PhoenixResponseJSON edit(@PathVariable Integer userId, @RequestBody EditUserBO userBO) {
        User user = new User();
        user.setId(userId);
        user.setMobile(userBO.getMobile());
        user.setEmail(userBO.getEmail());
        int count = userService.editUser(user);

        if (count > 0){
            return PhoenixResponseJSON.ok(user);
        }else {
            return PhoenixResponseJSON.errorMsg("修改用户信息出错！");
        }
    }

    @ApiOperation(value = "分配角色", notes = "分配角色", httpMethod = "PUT")
    @PutMapping("/{userId}/roles/{roleId}")
    public PhoenixResponseJSON edit(@PathVariable Integer userId, @PathVariable Integer roleId) {
        User user = new User();
        user.setId(userId);
        user.setRoleId(roleId);
        int count = userService.editUser(user);

        if (count > 0){
            return PhoenixResponseJSON.ok(user);
        }else {
            return PhoenixResponseJSON.errorMsg("分配角色出错！");
        }
    }

    @ApiOperation(value = "删除用户信息", notes = "删除用户信息", httpMethod = "DELETE")
    @DeleteMapping("/{userId}")
    public PhoenixResponseJSON delete(@PathVariable Integer userId) {
        int count = userService.removeUser(userId);
        if(count > 0) {
            return PhoenixResponseJSON.ok();
        }
        else {
            return PhoenixResponseJSON.errorMsg("删除用户信息出错！");
        }
    }

}
